<?php
/**
 * 模型基类
 */
class Model_Base {
	protected $_error;

	/**
	 * 链接数据库
	 * @param  [type] $connectionName database.ini中每个数据库链接的section名称,不传值使用配置的默认链接
	 * @return Zend_Db_Adapter_Abstract
	 */
	public function db($connectionName = "") {
		return Model_Db::getInstance($connectionName);
	}

	protected static function mysql($connectionName = '') {

		return Model_Db::getInstance($connectionName);
	}

	/**
	 * 设置错误
	 * @param  string $errorMsg  错误信息
	 * @param  [type] $errorCode 错误代码
	 * @return false
	 * @example return this->_error('错误信息','paramInvalid')
	 */
	protected function _error($errorMsg, $errorCode = "") {
		$this->_error = array("errorMsg" => $errorMsg, 'code' => $errorCode);
		return false;
	}

	/**
	 * 获取错误信息
	 * @return string
	 */
	public function getErrorMsg() {
		if (isset($this->_error) && isset($this->_error['errorMsg'])) {
			return $this->_error['errorMsg'];
		}
		return '';
	}

	/**
	 * 获取错误代码
	 * @return mix [description]
	 */
	public function getErrorCode() {
		if (isset($this->_error) && isset($this->_error['errorCode'])) {
			return $this->_error['errorCode'];
		}
		return null;
	}

	/**
	 * 批量插入
	 * @param  [type] $db     Zend_Db_Adapter实例
	 * @param  [type] $table  表名
	 * @param  array  $values 插入数据(关联数组)格式如下
	 * [
	 *    ['col1'=>'123','col2'=>'abc'],
	 *    ['col1'=>'345','col2'=>'qwe'],
	 * ]
	 * @return 影响行数
	 */
	public function insertBatch(Zend_Db_Adapter_Abstract $db, $table, array $values) {
		if (empty($values)) {
			return false;
		}
		$query = 'INSERT INTO ' . $db->quoteIdentifier($table);
		$keys = array_keys(current($values));
		sort($keys);
		$keys = array_map(array($db, 'quoteIdentifier'), $keys);
		$query .= " (" . implode(",", $keys) . ") VALUES";

		$queryVals = array();
		foreach ($values as $row) {

			ksort($row);

			foreach ($row as &$col) {
				$col = $db->quote($col);
			}

			$queryVals[] = '(' . implode(',', $row) . ')';
		}
		$query .= implode(',', $queryVals);

		$stm = $db->query($query);
		$affect_row = $stm->rowCount();
		return $affect_row;
	}

	/**
	 * 存在记录就更新，不存在就插入
	 * @param  [type] $db     Zend_Db_Adapter实例
	 * @param  [type] $table  表名
	 * @param  array  $values 插入或更新数据(关联数组)格式如下
	 * [
	 *    'col1'=>'123',
	 *    'col2'=>'abc'
	 * ]
	 * @return 影响行数
	 */
	static public function insertOrUpdate(Zend_Db_Adapter_Abstract $db, $table, array $data) {
		if (empty($data)) {
			return false;
		}
		$query = 'INSERT INTO ' . $db->quoteIdentifier($table);

		$filterData = [];
		$updateSql = "";
		foreach ($data as $_fields => $_value) {
			$fields = $db->quoteIdentifier($_fields);
			$value = $db->quote($_value);
			$filterData[$fields] = $value;
			$updateSql .= $fields . '=' . $value . ',';
		}
		$updateSql = rtrim($updateSql, ',');

		$fieldsStr = implode(',', array_keys($filterData));
		$valuesStr = implode(',', array_values($filterData));

		$query .= " (" . $fieldsStr . ") VALUES(" . $valuesStr . ") ON DUPLICATE KEY UPDATE " . $updateSql;
		$stm = $db->query($query);
		$affect_row = $stm->rowCount();
		return $affect_row;
	}
}